using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;

namespace HWAlarmGroupCreator.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                DisplayGraphics.Add(new HWDisplayGraphic(@"\\10.0.4.22\abstract\Graphics_E\P01_L1_DOILG_1001.htm"));
                DisplayGraphics.Add(new HWDisplayGraphic(@"\\10.0.4.22\abstract\Graphics_E\P01_L1_DOILG_1002.htm"));
                DisplayGraphics.Add(new HWDisplayGraphic(@"\\10.0.4.22\abstract\Graphics_E\P01_L1_DOILG_1003.htm"));
                DisplayGraphics.Add(new HWDisplayGraphic(@"\\10.0.4.22\abstract\Graphics_E\P01_L2_DOILD_H2O_TK_1330.htm"));
            }
            else
            {
            }
        }

        /// <summary>
        /// The <see cref="DisplayGraphics" /> property's name.
        /// </summary>
        public const string DisplayGraphicsPropertyName = "DisplayGraphics";

        private ObservableCollection<HWDisplayGraphic> _myProperty = new ObservableCollection<HWDisplayGraphic>();
        
        /// <summary>
        /// Sets and gets the DisplayGraphics property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<HWDisplayGraphic> DisplayGraphics
        {
            get
            {
                return _myProperty;
            }

            set
            {
                if (_myProperty == value)
                {
                    return;
                }

                RaisePropertyChanging(DisplayGraphicsPropertyName);
                _myProperty = value;
                RaisePropertyChanged(DisplayGraphicsPropertyName);
            }
        }

        private RelayCommand _addDisplaysCommand;

        /// <summary>
        /// Gets the AddDisplayCommand.
        /// </summary>
        public RelayCommand AddDisplayCommand
        {
            get
            {
                return _addDisplaysCommand
                    ?? (_addDisplaysCommand = new RelayCommand(() =>
                        {
                            var openFileDialog = new OpenFileDialog();
                            openFileDialog.Filter = "HMIWeb Displays (*.htm)|*.htm";
                            openFileDialog.Multiselect = true;
                            if (openFileDialog.ShowDialog()==true)
                                foreach (var fileName in openFileDialog.FileNames)
                                    DisplayGraphics.Add(new HWDisplayGraphic(fileName));
                        }));
            }
        }

        private RelayCommand _copyAlarmGroupCommand;

        /// <summary>
        /// Gets the CopyAlarmGroupCommand.
        /// </summary>
        public RelayCommand CopyAlarmGroupCommand
        {
            get
            {
                return _copyAlarmGroupCommand
                       ?? (_copyAlarmGroupCommand = new RelayCommand(() =>
                           {
                               var clipboardText = SelectedDisplayGraphic.Points
                                   .Where(x => x.ExportAllowed)
                                   .Aggregate("", (x, y) => x + y.PointName + Environment.NewLine);
                               Clipboard.SetText(clipboardText);
                           }));
            }
        }

        /// <summary>
        /// The <see cref="SelectedDisplayGraphic" /> property's name.
        /// </summary>
        public const string SelectedDisplayGraphicPropertyName = "SelectedDisplayGraphic";

        private HWDisplayGraphic _selectedDisplayGraphic;

        /// <summary>
        /// Sets and gets the SelectedDisplayGraphic property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public HWDisplayGraphic SelectedDisplayGraphic
        {
            get
            {   
                return _selectedDisplayGraphic;
            }

            set
            {
                if (_selectedDisplayGraphic == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedDisplayGraphicPropertyName);
                _selectedDisplayGraphic = value;
                RaisePropertyChanged(SelectedDisplayGraphicPropertyName);
            }
        }
    }
}